//# sourceURL=http://uMapApp/Module/StakeNum/StakeNumModule
define([
    "mhjs/mhjs"
    , "uMap/uMap"
    , "uEarth/uEarth"
    , "../Module"
    , "../../UI/Panel/FloatPanel"
    , "../../UI/Panel/PanelBlock/ListBlock"
    , "../../UI/Panel/PanelBlock/PropertiesBlock"
    , "../../UI/Panel/Item/ListItem"
    , "../../UI/Layer"
    , "./StakeNum"
], function (
    mhjs,
    uMap,
    uEarth,
    Module,
    FloatPanel,
    ListBlock,
    PropertiesBlock,
    ListItem,
    Layer,
    StakeNum
) {

    function StakeNumModule(option) {

        var _this = this;
        Module.call(_this, option);

        _this.title = "里程桩";
        _this.icon = _this.app.icons.stakeNum;

        _this.directions = {
            "U": {
                "label": "上行"
            },
            "D": {
                "label": "下行"
            },
            "UD": {
                "label": "双向"
            }
        };

        _this.panels = [];
        _this.items = [];

        _this.listPanel = new FloatPanel({
            "class": "rightPanel",
            "showClose": true
        });
        _this.listPanel.listBlock = new ListBlock({"class": "fullBlock"});
        _this.listPanel.listBlock.title = "里程桩";
        _this.listPanel.append(_this.listPanel.listBlock);

        _this.listPanel.on("close", function () {
            _this.closePanel();
        });

        _this.panels.push(_this.listPanel);

        // _this.detailPanel = new FloatPanel({
        //     "class": "leftPanel",
        //     "showClose": true
        // });
        // _this.detailPanel.propertiesBlock = new PropertiesBlock({"class": "fullBlock"});
        // _this.detailPanel.append(_this.detailPanel.propertiesBlock);
        //
        // _this.detailPanel.on("open", function () {
        //     _this.trigger("openDetail");
        // });
        //
        // _this.detailPanel.on("close", function () {
        //     _this.trigger("closeDetail");
        //     _this.unActiveAllItem();
        // });
        //
        // _this.panels.push(_this.detailPanel);


        _this.app.ui.layout.right.append(_this.listPanel);
        // _this.app.ui.layout.left.append(_this.detailPanel);

        // _this.on("loadInit", function () {
        //     _this.app.earth.on("click", function (result) {
        //         _this.getPositionStakeNum(result.pick && result.pick.position || result.position, function (stakeNum) {
        //             console.log(stakeNum);
        //         });
        //     });
        // });

    }

    var _class = StakeNumModule;

    mhjs.Inherit(_class, Module);

    Object.defineProperties(_class.prototype, {
        "data": {
            set: function (data) {
                var _this = this;
                _this.clear();
                _this._data = data;
                data && _this.renderList(data);
            },
            get: function () {
                return this._data;
            }
        },
        "renderList": {
            value: function (list) {

                var _this = this;

                _this.stakeNums = {
                    "numbers": {},
                    "directions": {}
                };

                $.each(list, function (i, item) {

                    if (_this.objMap) {
                        item = _this.objMap.format(item);
                    }

                    item.app = _this.app;
                    item.icon = _this.icon;
                    var stakeNum = new StakeNum(item);

                    _this.startStakeNum = _this.startStakeNum || stakeNum.stakeNum;
                    _this.stopStakeNum = _this.stopStakeNum || stakeNum.stakeNum;

                    if (_this.startStakeNum.distance > stakeNum.stakeNum.distance) {
                        _this.startStakeNum = stakeNum.stakeNum;
                    }

                    if (_this.stopStakeNum.distance < stakeNum.stakeNum.distance) {
                        _this.stopStakeNum = stakeNum.stakeNum;
                    }

                    _this.stakeNums.directions[stakeNum["朝向"]] = _this.stakeNums.directions[stakeNum["朝向"]] || {};
                    _this.stakeNums.directions[stakeNum["朝向"]][stakeNum.stakeNum.no] = stakeNum;

                    _this.stakeNums.numbers[stakeNum.stakeNum.no] = _this.stakeNums.numbers[stakeNum.stakeNum.no] || {};
                    _this.stakeNums.numbers[stakeNum.stakeNum.no][stakeNum["朝向"]] = stakeNum;

                    _this.items.push(stakeNum);

                    stakeNum.listItem = new ListItem({
                        "title": stakeNum["桩号"],
                        "subtitle": stakeNum["朝向"],
                        "icon": _this.icon.base64,
                        "stakeNum": stakeNum
                    });

                    stakeNum.propertiesBlock = new PropertiesBlock({"class":"row"});
                    stakeNum.propertiesBlock.title = stakeNum.title;
                    stakeNum.propertiesBlock.map = _this.propertiesMap;
                    stakeNum.propertiesBlock.properties = stakeNum;

                    stakeNum.marker.on("click", function () {
                        _this.activeItem(stakeNum);
                        _this.app.pop.target = stakeNum;
                        _this.app.pop.content = stakeNum.propertiesBlock;
                        _this.app.pop.position = stakeNum.position;
                        _this.app.pop.open();
                    });

                    stakeNum.on("close", function (item) {
                        if (_this.app.pop.target === item) {
                            _this.app.pop.close();
                        }
                    });

                    stakeNum.listItem.on("click", function (listitem) {
                        _this.activeItem(stakeNum);
                        stakeNum.layer.active();

                        // _this.detailPanel.propertiesBlock.title = stakeNum["桩号"];
                        // _this.detailPanel.propertiesBlock.map = _this.propertiesMap;
                        // _this.detailPanel.propertiesBlock.properties = stakeNum;
                        // _this.detailPanel.open();
                        //
                        // var westPoint = new uMap.Position({
                        //     "longitude": 120.8928408678179,
                        //     "latitude": 31.05035150199464
                        // });
                        // var eastPoint = new uMap.Position({
                        //     "longitude": 120.89425869893962,
                        //     "latitude": 31.050670775729905
                        // });


                        // (function (stakeNum) {
                        //
                        //     _this.app.modules.road.roadLineModule.loadInit(function () {
                        //         _this.app.earth.entities.add(new uMap.Entity({
                        //             objectOption: {
                        //                 position: _this.app.modules.road.roadLineModule.getSampledRoadCenterPositions(stakeNum),
                        //                 point   : new uMap.PointGraphic({
                        //                     objectOption: {
                        //                         color                     : ["#fff"]
                        //                         , disableDepthTestDistance: 0
                        //                     }
                        //                 }),
                        //                 label   : new uMap.LabelGraphic({
                        //                     objectOption: {
                        //                         text                      : stakeNum
                        //                         , verticalOrigin          : "BOTTOM"
                        //                         , pixelOffset             : [0, -10]
                        //                         , disableDepthTestDistance: Number.POSITIVE_INFINITY
                        //                     }
                        //                 })
                        //             }
                        //         }).object);
                        //     });
                        //
                        //
                        // })(stakeNum["桩号"]);

                        // if (_this.getCrossStakeNumPairPlanePoint(stakeNum.stakeNum.no, westPoint, eastPoint)) {
                        //     var crossPoint = _this.getCrossStakeNumPairPlanePoint(stakeNum.stakeNum.no, westPoint, eastPoint);
                        //     if (crossPoint) {
                        //
                        //         console.log(crossPoint);
                        //
                        //         _this.app.earth.entities.add(new uMap.Entity({
                        //             objectOption: {
                        //                 position: crossPoint,
                        //                 point   : new uMap.PointGraphic()
                        //             }
                        //         }).object);
                        //     }
                        //
                        // }


                    });

                    stakeNum.listItem.on("hide", function (listItem) {
                        listItem.stakeNum.close();
                    });

                    stakeNum.listItem.on("show", function (listItem) {
                        listItem.stakeNum.open();
                    });

                    _this.listPanel.listBlock.add(stakeNum.listItem);

                });


                _this.listPanel.listBlock.filter = {
                    "param": "stakeNum.searchKey"
                };

                _this.listPanel.listBlock.sorter = {
                    "param": {
                        "value": "stakeNum.sortKey",
                        "label": "桩号"
                    }
                };

            }
        },
        "clear": {
            value: function () {
                var _this = this;

                $.each(_this.items, function (i, item) {
                    item.close();
                    item.listItem.remove();
                });

                _this._data = undefined;
                _this.items = [];
            }
        },
        "objMap": {
            set: function (objMap) {
                var _this = this;
                _this._objMap = new mhjs.Map(objMap);
            },
            get: function () {
                return this._objMap;
            }
        },
        "propertiesMap": {
            set: function (propertiesMap) {
                var _this = this;
                _this._propertiesMap = new mhjs.Map(propertiesMap);
            },
            get: function () {
                return this._propertiesMap;
            }
        },
        "open": {
            value: function (forceReload) {
                var _this = this;

                _this.loadInit(function () {
                    _this.isOpened = true;
                    $.each(_this.items, function (i, item) {
                        item.open();
                    });
                    _this.trigger("open");
                    _this.trigger("toggle");
                }, forceReload);


            }
        },
        "loadInit": {
            value: function (callback, forceReload) {

                var _this = this;

                if (forceReload || !_this.isLoaded) {

                    _this.load({}, function (data, mapData) {

                        if (mapData) {
                            _this.objMap = mapData.obj;
                            _this.propertiesMap = mapData.properties;
                        }

                        _this.data = data;
                        _this.isLoaded = true;

                        _this.trigger("loadInit");

                        callback && callback();
                    });

                } else {
                    callback && callback();
                }


            }
        },
        "openPanel": {
            value: function () {
                var _this = this;
                if (!_this.isOpened) {
                    _this.open();
                }

                _this.listPanel.open();
                _this.isPanelOpened = true;
                _this.trigger("openPanel");

            }
        },
        "close": {
            value: function () {
                var _this = this;
                _this.closePanel(false);
                _this.unActiveAllItem();
                $.each(_this.items, function (i, item) {
                    item.close();
                });
                _this.isOpened = false;
                _this.trigger("close");
                _this.trigger("toggle");
            }
        },
        "activeItem": {
            value: function (item) {
                var _this = this;
                _this.unActiveAllItem();
                item.active();
                item.listItem.addClass("active");
                item.listItem.parent().animate({"scrollTop": item.listItem.dom[0].offsetTop});
            }
        },
        "unActiveAllItem": {
            value: function () {
                var _this = this;
                $.each(_this.items, function (i, item) {
                    item.active(false);
                    item.listItem.removeClass("active");
                });
            }
        },
        "getStakeNumPairPlane": {
            value: function (stakeNumNo) {

                var _this = this;
                if (_this.stakeNums.numbers[stakeNumNo] && _this.stakeNums.numbers[stakeNumNo][_this.directions["D"].label]) {
                    var upStake = _this.stakeNums.numbers[stakeNumNo][_this.directions["U"].label];
                    var downStake = _this.stakeNums.numbers[stakeNumNo][_this.directions["D"].label];
                    return uMap.Positions.getPointsPlane(downStake.position, upStake.position);
                }
            }
        },
        "getPositionStakeNum": {
            value: function (position) {

                var _this = this;
                position = new uMap.Position(position);
                var result;

                $.each(_this.stakeNums.numbers, function (stakeNumNo, stakeNums) {
                    if (_this.getStakeNumPairPlane(stakeNumNo)) {
                        var plane = _this.getStakeNumPairPlane(stakeNumNo);
                        var distance = uEarth.Plane.getPointDistance(plane, position.cartesian);
                        if (distance > 0 && distance < 1000) {
                            result = stakeNumNo * 1000 + distance;
                            return false;
                        }
                    }
                });

                if (result !== undefined) {
                    return StakeNum.transformDistance(result);
                }
            }
        },
        "getCrossStakeNumPairPlanePoint": {
            value: function (stakeNumNo, pointA, pointB) {

                var _this = this;

                pointA = new uMap.Position(pointA);
                pointB = new uMap.Position(pointB);

                if (_this.getStakeNumPairPlane(stakeNumNo)) {

                    var plane = _this.getStakeNumPairPlane(stakeNumNo);

                    var normal = uEarth.Cartesian3.subtract(pointB.cartesian, pointA.cartesian, new uEarth.Cartesian3());
                    uEarth.Cartesian3.normalize(normal, normal);

                    var distanceA = uEarth.Plane.getPointDistance(plane, pointA.cartesian);
                    var distanceB = uEarth.Plane.getPointDistance(plane, pointB.cartesian);

                    if (distanceA * distanceB > 0 && (Math.abs(distanceB) > Math.abs(distanceA))) {
                        uEarth.Cartesian3.negate(normal, normal);
                    }

                    var crossPoint = uEarth.IntersectionTests.rayPlane(new uEarth.Ray(pointA.cartesian, normal), plane);

                    if (crossPoint) {
                        return new uMap.Position(crossPoint);
                    }

                }

            }
        }
    });

    return _class;

});